define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wx/type/index',
                    add_url: 'wx/type/add',
                    edit_url: 'wx/type/edit',
                    del_url: 'wx/type/del',
                    multi_url: 'wx/type/multi',
                    table: 'type',
                }
            });
            var kinds = JSON.parse($(':hidden[name=kinds]').val());
            var table = $("#table");
            var click = function(){
                console.dir(1);
            };
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'type_id',
                sortName: 'type_id',
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'type_id', title: '分类ID', operate: false},
                        {field: 'type_name', title: '分类名称'},
                        {field: 'kind', title: '所属分类',formatter: Table.api.formatter.status, searchList: kinds, style: 'min-width:100px;'},
                        {field: 'click_pay', title: '设置点击付费', operate:false},
                        {field: 'see_pay', title: '设置付费查看', operate:false},
                        {field: 'cash_deposit', title: '设置保证金', operate:false},
                        {field: 'offer_reward', title: '设置悬赏', operate:false},
                        {field: 'transpond_pay', title: '设置有偿转发', operate:false},
                        {field: 'is_delete', title: '是否显示', operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                                {name: 'interest', text: '设置兴趣', title: '设置兴趣', icon: 'fa fa-circle', classname: 'btn btn-xs btn-default ', url: 'wx/type/interest_list'},
                                {
                                    name: 'testAjax',
                                    text: '测试',
                                    title: '测试',
                                    icon: 'fa fa-circle',
                                    classname: 'btn btn-xs btn-default btn-ajax',
                                    url: 'wx/type/test_ajax',
                                    success:function(data, ret){
                                        layer.confirm('is not?', {icon: 3, title:'提示'}, function(index){
                                            //do something

                                            layer.close(index);
                                        });
                                    },
                                    error:function(){
                                        console.dir(22);
                                    }
                                },
                            ]}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                search:false
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            var icons = JSON.parse($(':hidden[name=icons_json]').val());

            $(".sub").click(function(){
                commonAjaxSubmit();
                return false;
            });
            var num = 0;
            if(num == 0){
                add_option(num);
            }
            $(".add-info").click(function(){
                add_option(num);
            });
            function add_option(option_num){
                var option_num_next = option_num + 1;
                var _html = '<div id="option_'+option_num+'" style="height:500px; overflow: hidden; padding:0;margin:0"><table  class="table table-striped table-bordered table-hover dataTable" id="option_'+option_num+'">';
                _html += '<tr><th width="140">编辑信息详情【'+ option_num_next  +'】<input type="hidden" name="type_num['+ option_num +']" value="'+ option_num_next +'" /></th><td colspan="3" align="right"><span class="fa fa-remove"  title="删除"  onclick="del_opti on('+option_num+');"></span></td></tr>';
                _html += '<tr><th width="140">名称 ：</th><td colspan="3"><input type="text" name="option_name['+ option_num +']" value="" size="20" /></td></tr>';
                // _html += '<th width="140">是否作为推荐点 ：</th><td><label><input type="radio" name="is_interest[' + option_num + ']" value="0" />否</label>&nbsp;\n<label><input type="radio" name="is_interest[' + option_num + ']" value="1" />是</label></td></tr>';
                _html += '<tr><th width="140">类型 ：</th><td colspan="3"><label><input type="radio" name="option_type['+ option_num +']" onclick="show_sub($(this),'+ option_num +')" value="0"  />文本框</label> &nbsp;' +
                    '<label> <input type="radio" name="option_type['+ option_num +']" value="1" onclick="show_sub($(this),'+ option_num +')"  />文本域</label> &nbsp;\n' +
                    '<label> <input type="radio" name="option_type['+ option_num +']" value="2" onclick="show_sub($(this),'+ option_num +')"  />单选</label> &nbsp;\n' +
                    '<label> <input type="radio" name="option_type['+ option_num +']" value="3" onclick="show_sub($(this),'+ option_num +')"  />多选</label> &nbsp;\n' +
                    '<label> <input type="radio" name="option_type['+ option_num +']" value="4" onclick="show_sub($(this),'+ option_num +')"  />时间</label> &nbsp;\n' +
                    '<label> <input type="radio" name="option_type['+ option_num +']" value="5" onclick="show_sub($(this),'+ option_num +')"  />文件上传</label> &nbsp;\n' +
                    '<label> <input type="radio" name="option_type['+ option_num +']" value="6" onclick="show_sub($(this),'+ option_num +')"  />时间范围</label> &nbsp;</td></tr>';
                _html += '<tr><th width="140">单选/多选 的选项：</th><td colspan="3"><textarea name="data_list['+ option_num +']" cols="50"></textarea>（选项之间用“|”分割）</td></tr>';
                _html += '<tr><th width="140">是否显示名称 ：</th><td width="220"><label><input type="radio" name="is_show_name['+ option_num +']" value="0"  />不显示</label> &nbsp;<label> <input type="radio" name="is_show_name['+ option_num +']" value="1"  />显示</label></td>';
                _html += '<th width="140">是否必填 ：</th><td><label><input type="radio" name="is_must['+ option_num +']" value="0"  />否</label> &nbsp;<label> <input type="radio" name="is_must['+ option_num +']" value="1"  />是</label></td></tr>';
                _html += '<tr><th width="140">文本框/文本域<br /> 底纹描述文字：</th><td><input type="text" name="option_holder['+ option_num +']" value=""  /></td>';
                _html += '<th width="140">备注：</th><td><input type="text" name="option_notes['+ option_num +']" value=""  /></td></tr>';
                _html += '<tr><th width="140">是否允许用户自己填写：</th><td><label><input type="radio" name="is_self['+ option_num +']" value="0"  />不允许</label> &nbsp;<label> <input type="radio" name="is_self['+ option_num +']" value="1"  />允许</label></td>';
                _html += '<th width="140">自己填写后置/单位/规格/属性：</th><td><input type="text" name="self_suffix['+ option_num +']" value=""  /></td></tr>';
                _html += '<tr><th width="140">是否在详情页面显示</th><td><label><input type="radio" name="detail_show[' + option_num + ']" value="0" >不显示</label>&nbsp;&nbsp;<label><input type="radio" name="detail_show[' + option_num + ']">显示</label></td><th width="140">是否关联付费查看</th><td><label><input type="radio" name="pay_connect[' + option_num + ']" value="0">不关联</label>&nbsp;&nbsp;<label><input type="radio" name="pay_connect[' + option_num + ']" value="1">关联</label></td></tr>';
                _html += '<tr><th width="140">显示图标</th><td colspan="3">';
                for(icon of icons) {
                    _html += '<div style="display: inline-block">' +
                        '<label class="radio-inline">' +
                        '<img src="' + icon.path + '" alt="' + icon.title + '" style="position: relative;right: 10px;">' +
                        '<br>' +
                        '<input type="radio" name="icon[' + option_num + ']" value="' + icon.title + '"/>icon' + icon.title +
                        '</label>&nbsp;&nbsp;' +
                        '</div>';
                }
                _html += '</td></tr></table></div><br/>';
                $(".add_box").before(_html);
                num = option_num + 1;
            }
            Controller.api.bindevent();
        },
        edit: function () {
            var icons = JSON.parse($(':hidden[name=icons_json]').val());

            $(".sub").click(function(){
                commonAjaxSubmit();
                return false;
            });
            var num = parseInt($(':hidden[name=num]').val());
            if(num == 0){
                add_option(num);
            }
            $(".add-info").click(function(){
                add_option(num);
            });
            function add_option(option_num){
                var option_num_next = option_num + 1;
                var _html = '<div id="option_'+option_num+'" style="height:450px; overflow: hidden; padding:0;margin:0"><table  class="table table-striped table-bordered table-hover dataTable" id="option_'+option_num+'">';
                _html += '<tr><th width="140">编辑信息详情【'+ option_num_next  +'】<input type="hidden" name="type_num['+ option_num +']" value="'+ option_num_next +'" /></th><td colspan="3" align="right"><span class="fa fa-remove"  title="删除"  onclick="del_option('+option_num+');"></span></td></tr>';
                _html += '<tr><th width="140">名称 ：</th><td colspan="3"><input type="text" name="option_name['+ option_num +']" value="" size="20" /></td></tr>';
                // _html += '<th width="140">是否作为推荐点 ：</th><td><label><input type="radio" name="is_interest[' + option_num + ']" value="0" />否</label>&nbsp;\n<label><input type="radio" name="is_interest[' + option_num + ']" value="1" />是</label></td></tr>';
                _html += '<tr><th width="140">类型 ：</th><td colspan="3"><label><input type="radio" name="option_type['+ option_num +']" onclick="show_sub($(this),'+ option_num +')" value="0"  />文本框</label> &nbsp;' +
                    '<label> <input type="radio" name="option_type['+ option_num +']" value="1" onclick="show_sub($(this),'+ option_num +')"  />文本域</label> &nbsp;\n' +
                    '<label> <input type="radio" name="option_type['+ option_num +']" value="2" onclick="show_sub($(this),'+ option_num +')"  />单选</label> &nbsp;\n' +
                    '<label> <input type="radio" name="option_type['+ option_num +']" value="3" onclick="show_sub($(this),'+ option_num +')"  />多选</label> &nbsp;\n' +
                    '<label> <input type="radio" name="option_type['+ option_num +']" value="4" onclick="show_sub($(this),'+ option_num +')"  />时间</label> &nbsp;\n' +
                    '<label> <input type="radio" name="option_type['+ option_num +']" value="5" onclick="show_sub($(this),'+ option_num +')"  />文件上传</label> &nbsp;\n' +
                    '<label> <input type="radio" name="option_type['+ option_num +']" value="6" onclick="show_sub($(this),'+ option_num +')"  />时间范围</label> &nbsp;</td></tr>';
                _html += '<tr><th width="140">单选/多选 的选项：</th><td colspan="3"><textarea name="data_list['+ option_num +']" cols="50"></textarea>（选项之间用“|”分割）</td></tr>';

                _html += '<tr><th width="140">是否显示名称 ：</th><td width="220"><label><input type="radio" name="is_show_name['+ option_num +']" value="0"  />不显示</label> &nbsp;<label> <input type="radio" name="is_show_name['+ option_num +']" value="1"  />显示</label></td>';
                _html += '<th width="140">是否必填 ：</th><td><label><input type="radio" name="is_must['+ option_num +']" value="0"  />否</label> &nbsp;<label> <input type="radio" name="is_must['+ option_num +']" value="1"  />是</label></td></tr>';
                _html += '<tr><th width="140">文本框/文本域<br /> 底纹描述文字：</th><td><input type="text" name="option_holder['+ option_num +']" value=""  /></td>';
                _html += '<th width="140">备注：</th><td><input type="text" name="option_notes['+ option_num +']" value=""  /></td></tr>';
                _html += '<tr><th width="140">是否允许用户自己填写：</th><td><label><input type="radio" name="is_self['+ option_num +']" value="0"  />不允许</label> &nbsp;<label> <input type="radio" name="is_self['+ option_num +']" value="1"  />允许</label></td>';
                _html += '<th width="140">自己填写后置/单位/规格/属性：</th><td><input type="text" name="self_suffix['+ option_num +']" value=""  /></td>';
                _html += '<tr><th width="140">是否在详情页面显示</th><td><label><input type="radio" name="detail_show[' + option_num + ']" value="0" >不显示</label>&nbsp;&nbsp;<label><input type="radio" name="detail_show[' + option_num + ']">显示</label></td><th width="140">是否关联付费查看</th><td><label><input type="radio" name="pay_connect[' + option_num + ']" value="0">不关联</label>&nbsp;&nbsp;<label><input type="radio" name="pay_connect[' + option_num + ']" value="1">关联</label></td></tr>';
                _html += '<tr><th width="140">显示图标</th><td colspan="3">';
                for(icon of icons) {
                    _html += '<div style="display: inline-block">' +
                        '<label class="radio-inline">' +
                        '<img src="' + icon.path + '" alt="' + icon.title + '" style="position: relative;right: 10px;">' +
                        '<br>' +
                        '<input type="radio" name="icon[' + option_num + ']" value="' + icon.title + '"/>icon' + icon.title +
                        '</label>&nbsp;&nbsp;' +
                        '</div>';
                }
                _html += '</td></tr></table></div><br/>';
                $(".add_box").before(_html);
                num = option_num + 1;
            }
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        interest_list: function () {
            $('.btn_interest').click(function(){
               var interest_status = $(this).attr('data-isterest');
               var list_id = $(this).attr('data-id');
               var pars = {};
               pars.status = interest_status;
               pars.id = list_id;
               $.post('wx/type/change_interest', pars, function(dat){
                   console.dir(dat);
                   if(dat.message == 'success') {
                       layer.msg('更改成功', {
                           time: 1000
                       }, function(){
                           location.reload();
                       });
                   } else {
                       layer.msg('更改失败', {
                           time: 1000
                       }, function(){
                           location.reload();
                       });
                   }
               });
            });
        }
    };
    return Controller;
});